<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addDialog"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small">分配权限</el-button>
              <el-button size="small" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination layout="prev,pager,next" :total="total" :page-size="params.pagesize" @current-change="pageChange" />
        </el-row>
      </el-card>
    </div>
    <add-role-vue ref="addRole" :dialog-visible="dialogVisible" :update-list="getRoleList" @close-dialog="closeDilog" />
  </div>
</template>
<script>
import addRoleVue from './add-role.vue'
import { fetchRoleList, fetchDelRole } from '@/api/role'
export default {
  name: 'RoleIndex',
  components: { addRoleVue },
  data() {
    return {
      roleList: [],
      params: {
        page: 1,
        pagesize: 3
      },
      total: 0,
      dialogVisible: false
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await fetchRoleList(this.params)
      this.roleList = res.rows
      this.total = res.total
    },
    pageChange(page) {
      this.params.page = page
      this.getRoleList()
    },
    addDialog() {
      this.dialogVisible = true
    },
    closeDilog() {
      this.dialogVisible = false
    },
    delRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await fetchDelRole(id)
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {

      })
    },
    editRole(id) {
      this.dialogVisible = true
      this.$refs.addRole.getRoleDetail(id)
    }
  }
}
</script>
